﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Data.SqlClient;
using System.Net.Mail;
using System.Configuration;

public partial class admin_ContactCustomer : System.Web.UI.Page
{
    protected void Page_Load(object sender, EventArgs e)
    {
        LoadCustomers();
    }

    private void LoadCustomers()
    {
        using (SqlConnection conn = new SqlConnection(DAO.Connection.CONN_STRING1))
        {
            conn.Open();
            using (SqlCommand cmd = new SqlCommand("SELECT * from Fn_GetAllCust(0, 100000, 'id', 'asc')", conn))
            {
                SqlDataReader dr = cmd.ExecuteReader();
                if (dr != null)
                {
                    while (dr.Read())
                    {
                        ListItem l = new ListItem(dr["name"].ToString() + " " + dr["email"].ToString(), dr["email"].ToString());
                        l.Attributes.Add("_id", dr["id"].ToString());
                        ddl_customer.Items.Add(l);
                    }
                }
                dr.Close();
            }
        }
    }
    protected void btn_send_Click(object sender, EventArgs e)
    {
        if (Utilities.IsValidEmail(ddl_customer.SelectedValue.ToString()))
        {
            SendEmailToUser(ddl_customer.SelectedValue.ToString());
            Response.Redirect(Request.Url.ToString());
        }
    }

    private void SendEmailToUser(string p)
    {
        string senderMailAdress = ConfigurationManager.AppSettings["senderMailAdress"].ToString();
        string host = ConfigurationManager.AppSettings["host"].ToString();
        //string username = ConfigurationManager.AppSettings["username"].ToString();
        //string password = ConfigurationManager.AppSettings["password"].ToString();

        //create new mail message
        MailMessage mail = new MailMessage();

        //create mail message body
        mail.IsBodyHtml = true;
        mail.BodyEncoding = System.Text.Encoding.UTF8;

        mail.Body = uc_html_editor1.htmlCode;

        //set mail message properties
        mail.To.Add(new MailAddress(p));
        //mail.Bcc.Add("ratkop@gmail.com");
        mail.From = new MailAddress(senderMailAdress);
        mail.Subject = "TransportExchange.org notification";

        //create new SmtpClient
        SmtpClient smtpClient = new SmtpClient();

        //set SmtpClient properties
        smtpClient.Port = 25;
        smtpClient.Host = host;
        // for example gmail smtp server
        //smtpClient.Port = Convert.ToInt32(ConfigurationManager.AppSettings["port"]);
        string username = ConfigurationManager.AppSettings["username"].ToString();
        string password = ConfigurationManager.AppSettings["password"].ToString();
        smtpClient.Credentials = new System.Net.NetworkCredential(username, password);

        //send mail message
        try
        {
            smtpClient.Send(mail);
            Session["message"] = " The email has been send to the client<br />";
            Session["message_type_css"] = "par_message_ok";
        }
        catch (Exception ex)
        {
            Session["message"] = " An error occured while sending the mail. Reason " + ex.ToString();
            Session["message_type_css"] = "par_message_ok";
        }
    }
}
